package com.heritage.controller;

import com.heritage.dto.request.CommentRequest;
import com.heritage.dto.response.ApiResponse;
import com.heritage.dto.response.CommentResponse;
import com.heritage.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/heritage")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/{heritageId}/comments")
    public ResponseEntity<?> getCommentsByHeritageId(@PathVariable Long heritageId) {
        try {
            List<CommentResponse> comments = commentService.getCommentsByHeritageId(heritageId);
            return ResponseEntity.ok(ApiResponse.success(comments));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }

    @PostMapping("/{heritageId}/comment")
    public ResponseEntity<?> addComment(@PathVariable Long heritageId, @Valid @RequestBody CommentRequest commentRequest) {
        try {
            CommentResponse comment = commentService.addComment(heritageId, commentRequest);
            return ResponseEntity.ok(ApiResponse.success("评论发表成功", comment));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(ApiResponse.error(e.getMessage()));
        }
    }
}
